package com.TT.service.redis;

import com.TT.config.RedisConfig;
import com.TT.domain.redis.User;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by 苏文广 on 2018/11/22
 */
@Slf4j
@Service(value = "redisService")
public class RedisService{
    @Autowired
    private JedisPool jedisPool;

    private Jedis getJedis(){
        return jedisPool.getResource();
    }

    /**
     * 把User转JSON后存入Redis
     * @param user user
     * @return int
     */
    public void saveUserInRedis(User user) throws Exception {
        Jedis jedis = getJedis();
        try {
            jedis.set(RedisConfig.PREFIX+".user."+user.getUserName(), JSONObject.toJSONString(user));
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw e;
        }
    }
}
